package com.project.bean.dao.impl;

import com.project.bean.bean.StudentBean;
import com.project.bean.dao.IStudentDao;
import org.apache.ibatis.session.SqlSession;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class StudentDaoImpl extends BaseDao implements IStudentDao {
    public void add(StudentBean student) {
        SqlSession session =  this.getSession();
        //执行添加语句，第一个参数为命名空间.语句块的id,第二个参数为传入语句块的实参
        session.insert("studentScope.add", student);
        session.commit();
        session.close();
    }

    public void del(int id) {
        SqlSession session = this.getSession();
        session.delete("studentScope.del",id);
        session.commit();
        session.close();
    }

    public void update(int id, String tel) {
        Map map = new HashMap<>();
        map.put("sid", id);
        map.put("newTel", tel);
        SqlSession session = this.getSession();
        session.update("studentScope.update",map);
        session.commit();
        session.close();
    }

    public List<StudentBean> findAll() {
        SqlSession session = this.getSession();
        List<StudentBean> list = session.selectList("studentScope.findAll", null);
        session.close();
        return list;
    }

    public StudentBean findById(int id) {
        SqlSession session = this.getSession();
        StudentBean student = session.selectOne("studentScope.findById", id);
        session.close();
        return student;
    }

    public static void main(String[] args) {
        IStudentDao dao = new StudentDaoImpl();
//        dao.add(new StudentBean("刘秀", "13029384730", LocalDate.parse("1998-03-20")));
//        dao.del(11);
//        dao.update(1, "13903984302");
//        System.out.println(dao.findAll());
        System.out.println(dao.findById(1));
    }
}
